<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="6">
            <a-form-item label="订单号">
              <a-input placeholder="请输入订单号" v-model="queryParam.orderId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="退货单号">
              <a-input placeholder="请输入退货单号" v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="门店">
              <j-dict-select-tag v-model="queryParam.storeId" placeholder="请选择门店"
                                 dictCode="stm_store_info,name,store_id,status!=0"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :span="6">
              <a-form-item label="申请状态">
                <j-multi-select-tag v-model="queryParam.status_MultiString" placeholder="请选择申请状态"
                                    dictCode="return_apply_status"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="货品状态">
                <j-multi-select-tag v-model="queryParam.productStatus_MultiString" placeholder="请选择货品状态"
                                    dictCode="return_product_status"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="钱款状态">
                <j-multi-select-tag v-model="queryParam.moneyStatus_MultiString" placeholder="请选择钱款状态"
                                    dictCode="return_money_status"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="申请时间">
                <a-range-picker format="YYYY-MM-DD"
                                :placeholder="['开始时间', '结束时间']"
                                @change="onCreateTimeChange"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="包含商品">
                <a-auto-complete
                  v-model="queryParam.productSn"
                  :data-source="productSnDataSource"
                  placeholder="请输入商品货号"
                  :filter-option="filterOption"
                />
              </a-form-item>
            </a-col>
          </template>
          <a-col :span="6">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <!--<div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('退货管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>-->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <span>已选择</span>
        <a style="font-weight: 600">
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{ x: 1440 }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">查看</a>

          <!--<a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>-->
        </span>

        <table slot="orderItemName" slot-scope="text, record" class="orderItemTable">
          <tr v-for="(item, index) in record.omsOrderReturnItemList" :key="item.id">
            <td>{{item.productName}}</td>
          </tr>
        </table>
        <table slot="orderItemPrice" slot-scope="text, record" class="orderItemTable">
          <tr v-for="(item, index) in record.omsOrderReturnItemList" :key="item.id">
            <td>{{item.productPrice}}</td>
          </tr>
        </table>
        <table slot="orderItemQuantity" slot-scope="text, record" class="orderItemTable">
          <tr v-for="(item, index) in record.omsOrderReturnItemList" :key="item.id">
            <td>{{item.productCount}}</td>
          </tr>
        </table>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <stmOrderReturnApply-modal ref="modalForm" @ok="modalFormOk"/>

  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { httpAction, getAction } from '@/api/manage'
import StmOrderReturnApplyModal from './modules/StmOrderReturnApplyModal'
import JMultiSelectTag from '../../components/dict/JMultiSelectTag'

export default {
  name: 'StmOrderReturnApplyList',
  mixins: [JeecgListMixin],
  components: {
    StmOrderReturnApplyModal,
    JMultiSelectTag
  },
  data() {
    return {
      description: '门店退货管理管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 80,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        },
        {
          title: '门店',
          width: 120,
          align: 'center',
          dataIndex: 'storeId_dictText'
        },
        {
          title: '订单号',
          width: 160,
          align: 'center',
          dataIndex: 'orderId'
        },
        {
          title: '退货类型',
          align: 'center',
          dataIndex: 'returnType_dictText'
        },
        {
          title: '商品明细',
          align: 'center',
          children: [
            {
              title: '商品名称',
              width: 160,
              align: 'center',
              scopedSlots: { customRender: 'orderItemName' },
              customCell: (record) => {
                return {
                  'style': {
                    padding: 0,
                    margin: 0
                  }
                }
              }
            },
            {
              title: '单价',
              width: 60,
              align: 'center',
              scopedSlots: { customRender: 'orderItemPrice' },
              customCell: (record) => {
                return {
                  'style': {
                    padding: 0,
                    margin: 0
                  }
                }
              }
            },
            {
              title: '数量',
              width: 60,
              align: 'center',
              scopedSlots: { customRender: 'orderItemQuantity' },
              customCell: (record) => {
                return {
                  'style': {
                    padding: 0,
                    margin: 0
                  }
                }
              }
            }]
        },
        {
          title: '申请状态',
          align: 'center',
          dataIndex: 'status_dictText'
        },
        {
          title: '货品状态',
          align: 'center',
          dataIndex: 'productStatus_dictText'
        },
        {
          title: '钱款状态',
          align: 'center',
          dataIndex: 'moneyStatus_dictText'
        },
        {
          title: '会员',
          align: 'center',
          dataIndex: 'memberId'
        },
        {
          title: '退款金额',
          align: 'center',
          dataIndex: 'returnAmount'
        },
        {
          title: '扣除费用',
          align: 'center',
          dataIndex: 'deductAmount'
        },
        {
          title: '申请人',
          width: 120,
          align: 'center',
          dataIndex: 'returnName'
        },
        {
          title: '申请时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '退货系统',
          align: 'center',
          dataIndex: 'sourceSystem_dictText'
        }
      ],
      productSnDataSource: [],
      // 请求参数
      url: {
        list: '/stm/omsOrderReturnApply/listWithItem',
        queryList: '/pms/pmsProduct/queryList',
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.getProduct()
  },
  methods: {
    getProduct() {
      getAction(this.url.queryList).then(res => {
        if (res.success) {
          console.log('ProductData => ' + res.result)
          this.productSnDataSource = [...new Set(res.result.map(data => (data['productSn'])))]
        } else {
          console.log(res.message)
        }
      })
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLocaleLowerCase().indexOf(input.toLocaleLowerCase()) >= 0
      )
    },
    onCreateTimeChange: function(value, dateString) {
      console.log(dateString[0], dateString[1])
      this.queryParam.createTime_begin = dateString[0]
      this.queryParam.createTime_end = dateString[1]
    },
    initDictConfig() {
    }

  }
}
</script>
<style scoped>

.orderItemTable {
  width: 100%;
  height: 100%;
  border: 1px;
}

.orderItemTable td {
  border-bottom: #e8e8e8 1px solid;
  text-align: center;
}

.orderItemTable tr:last-child td {
  border-bottom: #fff 1px solid;
}

@import '~@assets/less/common.less'
</style>